﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


namespace Shut.UIControls
{
	[
	AspNetHostingPermission(SecurityAction.Demand,
		Level = AspNetHostingPermissionLevel.Minimal),
	AspNetHostingPermission(SecurityAction.InheritanceDemand,
		Level = AspNetHostingPermissionLevel.Minimal)
	]
	public class SiteMapNavigation : WebControl
	{

		public static SiteMapProvider CurrentSiteMapProvider
		{
			get
			{
				return TocNode.CurrentSiteMapProvider;

			}
		}

		public static string ShutProvider
		{
			get
			{
				return TocNode.ShutProvider;
			}
		}
		private HtmlGenericControl ContainerTag;

		public String currentPageGroup { get; set; }


		public SiteMapNavigation()
		{
			// this can be overridden by page
			// if currentnode doesnt exist, no group should be displayed
			//if (CurrentSiteMapProvider.CurrentNode != null) currentPageGroup = TocNode.GetPageGroup(CurrentSiteMapProvider.CurrentNode);
			currentPageGroup = TocNode.CurrentNode.PageGroup;

		}
		public override void RenderBeginTag(HtmlTextWriter writer)
		{

			//base.RenderBeginTag(writer);
		}
		public override void RenderEndTag(HtmlTextWriter writer)
		{

			//base.RenderEndTag(writer);
		}
		protected override void RenderContents(HtmlTextWriter writer)
		{
			// render the sitemap
			ContainerTag = new HtmlGenericControl("ul");
			ContainerTag.Attributes["class"] = CssClass;
			SiteMapNodeCollection smNodes = CurrentSiteMapProvider.RootNode.ChildNodes;
			CreateNodes(smNodes, ContainerTag);


			ContainerTag.RenderControl(writer);

		}

		protected void CreateNodes(SiteMapNodeCollection smnodes, HtmlGenericControl ulParent)
		{
			// create an LI for every sitemapnode

			foreach (SiteMapNode node in smnodes)
			{


				TocNode tn = TocNode.GetNode(node["key"]);
				if (null == tn)
				{
					// if no key, try searching by url
					tn = TocNode.GetNodeByUrl(node.Url);
				}

				if (!tn.Visible)
				{
					continue;
				}


				HtmlGenericControl newNode = CreateNode(tn);
				// insert newnode inside parent object
				ulParent.Controls.Add(newNode);

				if (node.HasChildNodes)
				{
					// advance depth
					// recursive call, create a new ul
					HtmlGenericControl ulP = new HtmlGenericControl("ul");
					if (!string.IsNullOrEmpty(tn.ListClass))
					{
						ulP.Attributes["class"] = tn.ListClass;
					}
					CreateNodes(node.ChildNodes, ulP);
					// add ul to li
					newNode.Controls.Add(ulP);

				}
			}


		}

		private HtmlGenericControl CreateNode(TocNode smnode)
		{

			// return node
			HtmlGenericControl newNode = new HtmlGenericControl("li");

			if (smnode.Url == String.Empty)
			{
				HtmlGenericControl newSpan = new HtmlGenericControl("span");
				newSpan.InnerText = smnode.Title; // this should be a literal resource for translation purposes
				newNode.Controls.Add(newSpan);

			}
			else
			{
				HtmlAnchor newUrl = new HtmlAnchor();
				newUrl.HRef = smnode.Url;
				newUrl.InnerText = smnode.Title;
				newUrl.Title = smnode.Description;
				newNode.Controls.Add(newUrl);

				if (TocNode.CurrentNode.Key == smnode.Key)
				{
					newUrl.Attributes["class"] += " itemselected";

				}
			}
			if (!string.IsNullOrEmpty(smnode.ItemClass))
			{
				newNode.Attributes["class"] = smnode.ItemClass;
			}
			if (smnode.IsParent)
			{
				//  compare to cPageGroup or currentPageGroup if exists

				if (currentPageGroup == smnode.PageGroup)
				{
					newNode.Attributes["class"] += " selected";
				}
			}

			return newNode;
		}


	}



}
